package com.smyh.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.smyh.UIM.R;
import com.smyh.bean.DeviceInfo;

import java.util.List;

/**
 * Created by 姜龙 on 2016/6/28.
 */
public class AddrItemAdapter extends ArrayAdapter<DeviceInfo> {
    private int resourceId;
    float startX = 0;
    public AddrItemAdapter(Context context, int resource, List objects) {
        super(context, resource, objects);
        resourceId = resource;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DeviceInfo item = getItem(position);
        View view;
        final ViewHolder viewHolder;
        if(convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resourceId,null);

            viewHolder = new ViewHolder();
            viewHolder.device_addr  = (TextView) view.findViewById(R.id.device_addr);
            viewHolder.device_type  = (TextView) view.findViewById(R.id.device_type);
            viewHolder.is_online_img  = (ImageView) view.findViewById(R.id.is_online_img);
            viewHolder.connection_toggle  = (ToggleButton) view.findViewById(R.id.connection_toggle);
            viewHolder.addr_item  = (LinearLayout) view.findViewById(R.id.addr_item);
            view.setTag(viewHolder);
        }else{
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }

        viewHolder.device_addr.setText(item.getDeviceAddr());
        viewHolder.device_type.setText(item.getDeviceType());
        if(item.getOnline()){
            viewHolder.is_online_img.setImageResource(R.drawable.green);
        }else{
            viewHolder.is_online_img.setImageResource(R.drawable.red);
        }
        if(item.getConnection()){
            viewHolder.connection_toggle.setChecked(true);
        }else{
            viewHolder.connection_toggle.setChecked(false);
        }
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                LinearLayout.LayoutParams leftMenuParams =new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        startX = event.getX();
                        Log.i("MotionEventDown",startX+"");
                    case  MotionEvent.ACTION_MOVE:
                        Log.i("MotionEventMove",startX +"   " + event.getX() );
                        if(startX - event.getX()>160){
                            leftMenuParams.leftMargin = -320;
                            viewHolder.addr_item.setLayoutParams(leftMenuParams);
                        }else if(event.getX() - startX >50){
                            leftMenuParams.leftMargin = 0;
                            viewHolder.addr_item.setLayoutParams(leftMenuParams);
                        }else{
                            leftMenuParams.leftMargin = (int) (event.getX() - startX);
                            viewHolder.addr_item.setLayoutParams(leftMenuParams);
                        }
                        break;
                }
                return true;
            }
        });

        return view;
    }

    class ViewHolder{
        ToggleButton connection_toggle;
        TextView device_addr;
        TextView device_type;
        ImageView is_online_img;
        LinearLayout addr_item;
    }

}
